/*
 * @Author: szx
 * @Date: 2022-01-28 14:40:33
 * @LastEditTime: 2022-01-28 14:43:43
 * @Description:
 * @FilePath: \leetcode\500-599\503\503.js
 */
/**
 * @param {number[]} nums
 * @return {number[]}
 */
var nextGreaterElements = function (nums) {
    const n = nums.length;
    const res = new Array(n).fill(0);
    const st = [0];
    //单调栈
    for (let i = 1; i < 2 * n; i++) {
        let top = st[st.length - 1];
        if (nums[i] <= nums[top]) {
            st.push(i);
        } else {
            while (st.length && nums[i] > nums[st[st.length - 1]]) {
                top = st.pop();
                if (top >= n) return res;
                res[top] = i - top;
            }
            st.push(i);
        }
    }
    return res;
};
